<?php
namespace app\yticnus\Controller;
class Member extends Common {

	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();
	}

	/*
	*	编辑用户界面
	*/
	public function edit(){
        if(request()->isPost()){
			$integral = input("integral");
            $wheel_time = input("wheel_time");
            $egg_time = input("egg_time");
            $shake_time = input('shake_time');
            $data['integral'] = $integral;
            $data['wheel_time'] = $wheel_time;
            $data['egg_time'] = $egg_time;
            $data['shake_time'] = $shake_time;
			
			$id=input('id');
            db('member')->where("id='$id'")->update($data);
			$this->success("操作成功!",url('member/index'));
		}
		$id  = input("id");
		$res = db('member')->where("id='$id'")->find();
		$this->assign("res",$res);
		return 	$this->fetch();
    }
    
	/*
	*	用户列表
	*/
	public function index(){
		if(request()->isPost()){
			$keyword = input('keyword');
			$list = db('member')->where('nickname',$keyword)->paginate(10);
			$this->assign('data',$list);
			return $this->fetch();
		}
		$list = db('member')->paginate(10)->each(function($item, $key){
			if($item['vtime'] < time()){
				$item['wheel_time'] = 0;
				$item['egg_time'] = 0;
				$item['shake_time'] = 0;
			}
			return $item;
		});
		$this->assign('data',$list);
		return $this->fetch();
	}

	/*
	*	删除用户
	*/
	public function del(){
		$id = input("id");
		if(!$id) $this->error('参数有误');
		$da=db('member')->where("id='$id'")->find();
        if(!$da) $this->error('用户不存在');
		$res=db('member')->where("id='$id'")->delete();
		if(!$res) $this->error('删除失败');
		$this->success("删除成功!",url('member/index'));
    }
    
    /*
    *	禁用用户
    */
	public function ban(){
		$id = input("id");
		if(!$id) $this->error('参数有误');
		$da=db('member')->where("id='$id'")->find();
		if(!$da) $this->error('用户不存在');
		if($da['status']==1){
			$res=db('member')->where("id='$id'")->update(["status"=>0]);
		}else{
			$res=db('member')->where("id='$id'")->update(["status"=>1]);
		}
		$this->success("操作成功!",url('member/index'));
	}

    /*
    *	导入数据
	*/
	public function add(){
		return $this->fetch();
	}

	/*
	*  处理导入数据
	*/
	public function export(){
		date_default_timezone_set('PRC');
		set_time_limit(0);   // 设置脚本最大执行时间 为0 永不过期
		if(request()->isPost()){
            $re = vendor("Excel.PHPExcel");
			$objPHPExcel = new \PHPExcel();

			$file = request()->file('file');
			$vtime = input('vtime');

			if($file){
				$info = $file->validate(['ext'=>'xlsx,xls'])->move(ROOT_PATH . 'public/static/uploads');
				if($info){
					$exclePath = $info->getSaveName();  //获取文件名
					$file_suffix_name = explode('.',$exclePath);
					$file_suffix_name = $file_suffix_name[1];
					
					$file_name = ROOT_PATH . 'public/static/uploads/'. $exclePath;   //上传文件的地址
                    $startRow = 2;
                    $endRow = 501;
					$bool = true;
					
					//分批导入
					do{
						//excle处理
						$excel_array = $this->array_excle($file_name, $file_suffix_name, $startRow, $endRow);

						//数组处理
						$bool = $this->array_iconv($excel_array,$vtime);

						$startRow += 500;
						$endRow += 500;
						ob_flush();
						flush();
						usleep(1000); //睡会觉，大量读取，冲击挺大
					}while($bool);

					$this->success("操作成功!", url('member/index'));
				}else{  
                    // 上传失败获取错误信息 
                    return $this->error($file->getError());
                }
			}else{
                return $this->error('请添加文件');
            }
		}
	}

    /**
    *excle处理
    */
    function array_excle($file_name, $file_suffix_name, $startRow = 1, $endRow = null){
        ini_set('memory_limit','4072M');    // 临时设置最大内存占用为3G
        set_time_limit(0);   // 设置脚本最大执行时间 为0 永不过期

        if($file_suffix_name == 'xlsx'){
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        }else{  
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        }

        $objReader->setReadDataOnly(true);

        //如果有指定行数，则设置过滤器
        if ($startRow && $endRow) {
            vendor("Excel.PHPExcels");
            $perf = new \PHPExcels();
            $perf->startRow = $startRow;
            $perf->endRow   = $endRow;
            $objReader->setReadFilter($perf);
        }

        $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8  
        $activeSheet = $obj_PHPExcel->getActiveSheet();
        $total = $activeSheet->getHighestRow(); //总行数
        
        $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
        return $excel_array;
	}
	
    /**
     *数组处理
     */
    function array_iconv($array,$vtime)
    {
		date_default_timezone_set('PRC');
		ini_set('memory_limit','4072M');    // 临时设置最大内存占用为3G
		set_time_limit(0);   // 设置脚本最大执行时间 为0 永不过期
		$data = array();
		$bool = true;

		if(is_array($array)){
			foreach ($array as $key => &$value) {
				if(count($value)<4){
					return false;
				}
				if(!empty($value[0])){
					$re = db('member')->where('nickname',$value[0])->find();
					if($re){
						$data['wheel_time'] = !empty($value[1])?$value[1]:0;
						$data['egg_time'] = !empty($value[2])?$value[1]:0;
						$data['shake_time'] = !empty($value[3])?$value[1]:0;
						$data['vtime'] = time()+$vtime*60*60; 
						db('member')->where('nickname',$value[0])->update($data);
					}
				}elseif($value[0] == 0){
					$re = db('member')->where('nickname',$value[0])->find();
					if($re){
						$data['wheel_time'] = !empty($value[1])?$value[1]:0;
						$data['egg_time'] = !empty($value[2])?$value[1]:0;
						$data['shake_time'] = !empty($value[3])?$value[1]:0;
						$data['vtime'] = time()+$vtime*60*60; 
						db('member')->where('nickname',$value[0])->update($data);
					}
				}
			}	
			return true;
		}else{
			return false;
		}
	}
}